package com.wzxy.ping.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.wzxy.ping.bean.Function;
import com.wzxy.ping.bean.User;
import com.wzxy.ping.service.FunctionService;
import com.wzxy.ping.service.UserService;

@Component("loginAction")
@Scope("prototype")
public class LoginAction extends SuperController{

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private FunctionService functionService;
	
	@Action(value = "isLogin")
	public void isLogin() throws Exception{
		Map<String, String> map = getParamMap();
		if(map.get("admin")!=null&&map.get("password")!=null){
			User user = userService.byAdminAndPassword(map);
			System.out.println(user);
			if(user!=null){
				getRequest().getSession().setAttribute(user.getUserid(), user.getRole());
				getResponse().getWriter().print(user.getUserid());
				List<Function> list = functionService.getFuntionByRole(user.getRole());
				getSession().setAttribute("list", list);
			}else{
				getResponse().getWriter().print(0);
			}
		}else{
			getResponse().getWriter().print(0);
		}
	}
	
	
	@Action(value = "issucee",results = {@Result(location="/main.html")})
	public String issuccess() throws Exception{
		Map<String, String> map = getParamMap();
		String role = (String) getRequest().getSession().getAttribute(map.get("userid"));
		List<Function> list = functionService.getFuntionByRole(role);
		getSession().setAttribute("list", list);
		return "success";
	}
	
}
